﻿using System.Reflection;

namespace Leo.Common.Json
{
    internal class TypeDescriptor
    {
        private readonly Type objectType;
        public TypeDescriptor(Type objectType)
        {
            this.objectType = objectType;
            PropertyInfo[] properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            this.PropertyCount = properties.Length;
            this.Properties = Enumerable.ToList<TypePropertyDescriptor>(Enumerable.Select<PropertyInfo, TypePropertyDescriptor>(properties, (PropertyInfo p) => new TypePropertyDescriptor(p)));
        }
         
        public int PropertyCount { get; } 

        public List<TypePropertyDescriptor> Properties { get; set; }


        public bool HasProperty(string propertyName)
        {
            return Enumerable.Any<TypePropertyDescriptor>(this.Properties, (TypePropertyDescriptor p) => p.IsProperty(propertyName));
        }

    }
}
